<?php

/**
 * @file IndexController
 * @author garypradoruiz <garypradoruiz@gmail.com>
 * @link mailto:garypradoruiz@gmail.com
 * @copyleft 2012 garypradoruiz
 * @license GPL
 * @version 1
 */
class Departament_IndexController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {
        $departamentDb = new Departament_Model_DbTable_Departament();

        $select = $departamentDb->select()->setIntegrityCheck(false)
                        ->from(array('d' => 'departament'), array('id', 'name', 'description'))
                        ->join(array('u' => 'user'), 'd.id_user = u.id', array('name_user' => 'nombre'))->where('d.deleted=?', 0);

        $result = $departamentDb->fetchAll($select);
        $this->view->departaments = $result;
    }

    public function addDepartamentAction() {
        $departamentDb = new Departament_Model_DbTable_Departament();
        $request = $this->getRequest();

        if ($request->isPost()) {
            $data = $request->getPost();
            //$data['departament_job'] = implode(',', $data['departament_job']);

            $departamentDb->insert($data);
            $this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Departamento adicionado con exito.');
            $this->_helper->redirector->gotoUrlAndExit('departament/index/add-departament/');
        } else {
            $jobsDepartament = $this->getDepartamentJobs();
            $this->view->jobsDepartament = $jobsDepartament;
            $this->getAllUserDep();
        }
    }

    public function editDepartamentAction() {
        $id = $this->_getParam('id', 0);
        $departamentDb = new Departament_Model_DbTable_Departament();

        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost();
            $where = $departamentDb->getAdapter()->quoteInto('id=?', $id);
            $data['update_date'] = date('Y-m-d H:i:s');
            //$data['departament_job'] = implode(',', $data['departament_job']);

            $departamentDb->update($data, $where);

            $this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Departamento editado con exito.');
            $this->_helper->redirector->gotoUrlAndExit('departament/index/edit-departament/id/' . $id);
        } else {
            $dataDepartament = $departamentDb->fetchRow('id=' . $id);
            $jobsDepartament = $this->getDepartamentJobs();

            if (!$dataDepartament) {
                throw new Exception("Fila no encontrada $id");
            } else {
                $this->view->departament = $dataDepartament;
                $this->view->jobsDepartament = $jobsDepartament;
            }
            $this->getAllUserDep();
        }
    }

    public function deleteDepartamentAction() {
        $id = $this->_getParam('id', 0);
        $departamentDb = new Departament_Model_DbTable_Departament();
        $where = $departamentDb->getAdapter()->quoteInto('id=?', $id);

        $data = array('deleted' => 1);
        $departamentDb->update($data, $where);

        $this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Departamento eliminado con exito.');
        $this->_helper->redirector->gotoUrlAndExit('departament/');
    }

    public function getDepartamentJobs() {
        $dpJobDB = new Departament_Model_DbTable_DepartamentJob();
        $all_job_departament = $dpJobDB->fetchAll(null, 'id DESC');

        return $all_job_departament;
    }

    public function comboAction() {
    }

    private function getAllUserDep() {
        $userDb = new User_Model_DbTable_User();
        $select = $userDb->select()->where('type=?', 'DEPARTAMENT');
        $this->view->users = $userDb->fetchAll($select);
    }

}

?>
